(function(window){

    function Lyric(path){
        return new Lyric.prototype.init(path);
    }
   Lyric.prototype = {
        constructor:Lyric,
        times:[],
        lyrics:[],
        index:-1,
       
        init:function(path){
            this.path = path;
       
        },
        loadLyric:function(callBack){
            var $this = this;
            $.ajax({
                url: $this.path,
                type: "GET",
                dataType: "text",
                async :true,
                success: function(data){
                    $this.parseLyric(data);
                    callBack()
                },
                error: function(e){
                    console.log(e)
            }
        });
        },
        parseLyric:function(data){

            var $this = this;
            $this.lyrics = [];
            $this.times = [];
            var array = data.split("\n")
            // console.log(array)
            //遍历歌词[01:06.25]
            var timeReg = /\[(\d+:\d+\.\d+)\]/;

            $.each(array, function(index, ele){


                //处理歌词[null]
                // 
               
                var lrc = ele.split("]")[1];
                // console.log(lrc)
                if(lrc.length == 1) return true;
                // console.log(typeof lrc)
                $this.lyrics.push(lrc);



                var res = timeReg.exec(ele);
                // console.log(res)
                if(res==null) return true;
                var timeStr = res[1]
                // console.log(timeStr)
                var res2 = timeStr.split(":")
                var min = parseInt(res2[0] * 60);
                var sec = parseFloat(res2[1]);
                var times = Number(min+sec);
                    time = parseFloat(Number(min+sec).toFixed(2));//两位小数
                // console.log(time)
                $this.times.push(time);

            });
                            // console.log($this.lyrics)
                // console.log($this.lyrics[0])
                 // $this.lyrics = [];
        },
        currentIndex:function(currentTime){
            // console.log(currentTime)
            if(currentTime>= this.times[0]){
                // console.log(currentTime, this.times[0])
                this.index++;
                this.times.shift();//删除数组第一个
            }
            return this.index;
        }


    }
    Lyric.prototype.init.prototype =Lyric.prototype;
    window.Lyric =Lyric;

})(window);